<?php

class OzdorovlenieController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
    	 public function filters()
	{
		return array(
			'accessControl',
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
			      'actions' => array('login'),
			      'users' => array('*'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('add','index','edit','delete'),
				'roles'=>array('admin'),
			),
			array('deny',
			      'users' => array('*'),
			),
		);
	}

	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	public function actionIndex()
	{
		$this->pagetitle = 'Оздоровление детей - '.Yii::app()->name;
		$criteria = new CDbCriteria;
        $criteria->condition = "type='1'";
		$criteria->order = 'date_public DESC';
		$model = News::model()->findAll($criteria);
		$this->render('list', array('model'=>$model));
	}

    public function actionView($id)
    {
        $model = News::model()->findByPk($id);
        $this->pagetitle = $model->title.' - '.Yii::app()->name;
        $this->render('view', array('model' => $model));
    }

    public function actionAdd()
    {
        $model = new News;
        if(isset($_POST['news']))
        {
            $model->attributes = $_POST['news'];
            $model->type = 1;
            $model->date_public = date('Y.m.d h:i:s',strtotime($model->date_public));
            $model->file = CUploadedFile::getInstance($model,'file');
            if($model->validate())
            {
                if(isset($model->file))
                {
                    $file_name = $this->uniqueName($model->file->name);
                    $model->file->saveAs(dirname(Yii::app()->getBasePath()) .'/uploads/' . $file_name);
                    $model->file = $file_name;
                }
                $model->save();
                $this->redirect(array('/ozdorovlenie'));
            }
        }
        $this->render('form', array('model' => $model));
    }

    public function actionEdit($id)
    {
        $model = News::model()->findByPk($id);
        if(isset($_POST['news']))
        {
            $old_file = $model->file;
            $model->attributes = $_POST['news'];
            $model->type = 1;
            $model->date_public = date('Y.m.d h:i:s',strtotime($model->date_public));
            $model->file = CUploadedFile::getInstance($model,'file');
            if($model->validate())
            {
                if(isset($model->file) AND $model->file!=$old_file AND $model->file!=null)
                {
                    $file_name = $this->uniqueName($model->file->name);
                    $model->file->saveAs(dirname(Yii::app()->getBasePath()) .'/uploads/' . $file_name);
                    $model->file = $file_name;
                    @unlink(dirname(Yii::app()->getBasePath()) .'/uploads/' . $old_file);
                }
                else{
                    $model->file = $old_file;
                }
                $model->save();
                $this->redirect(array('/ozdorovlenie'));
            }
        }
        $this->render('form', array('model' => $model));

    }

    public function actionDelete($id)
    {
        $model = News::model()->findByPk($id);
        if(isset($model->file) AND $model->file!=null)
        {
            @unlink(dirname(Yii::app()->getBasePath()) .'/uploads/' . $model->file);
        }
        News::model()->deleteByPk($id);
        $this->redirect(array('/ozdorovlenie'));
    }

    private function uniqueName($fname)
	{
		$ext = end(explode(".",$fname));
		return md5(uniqid()).'.'.$ext;
	}

}